package cutetd.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cutetd.dao.NaviDao;
import cutetd.model.M_Navi;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NaviAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private M_Navi navi;
	private String keyString;
	private List<M_Navi> naviList = new ArrayList<M_Navi>();
	private NaviDao naviDao = new NaviDao();

	public String manager() {
		// getNaviList();

		// generate menu list within <ul> <li>tags
		/*
		 * for (int i = 0; i < naviList.size(); ++i) { M_Navi parent =
		 * naviList.get(i); if (parent.getParentKey().equals("")) {
		 * parent.setDisplayName("<li>" + parent.getDisplayName());
		 * naviMenu.add(parent);
		 * 
		 * boolean ulMark = true; for (int j = 0; j < naviList.size(); ++j) {
		 * M_Navi child = naviList.get(j); if
		 * (child.getParentKey().equals(parent.getKeyString())) { if (ulMark) {
		 * ulMark = false; child.setDisplayName("<ul><li>" +
		 * child.getDisplayName() + "</li>"); } else {
		 * child.setDisplayName("<li>" + child.getDisplayName() + "</li>"); }
		 * naviMenu.add(child); // System.out.println(child.getDisplayName()); }
		 * }
		 * 
		 * M_Navi last = naviMenu.get(naviMenu.size() - 1); if (!ulMark) {
		 * last.setDisplayName(last.getDisplayName() + "</ul>"); } else {
		 * last.setDisplayName(last.getDisplayName() + "</li>"); }
		 * naviMenu.remove(naviMenu.size() - 1); naviMenu.add(last); } }
		 */
		/*
		 * if (naviList == null) { System.out.println("naviList is null"); } for
		 * (int i = 0; i < naviList.size(); ++i) {
		 * System.out.println(naviList.get(i).getDisplayName()); }
		 */
		return SUCCESS;
	}

	public String addNavi() {
		if (navi == null) {
			System.out.println("navi is null");
			return "manager";
		}
		
		if (navi.getParentKey() == null) {
			navi.setParentKey("");
		} else {
			navi.setParentKey(navi.getParentKey().trim());
		}
		navi.setDisplayName(navi.getDisplayName().trim());

		naviDao.saveGetObject(navi);
		return "manager";
	}

	public String updateNavi() {
		naviDao.saveOrUpdate(navi);
		return "manager";
	}

	public String deleteNavi() {
		if (keyString != null) {
			M_Navi t = (M_Navi) naviDao.getObjectByKey(M_Navi.class, keyString);
			naviList = naviDao.getListAll();

			for (int i = 0; i < naviList.size(); ++i) {
				if (t.getKeyString().equals(naviList.get(i).getParentKey())) {
					System.out.println("have child nodes,Can't delete!");
					@SuppressWarnings("unchecked")
					Map<String, Object> session = ActionContext.getContext().getSession();
					session.put("OperaterInfo", "有子节点在，不能删除");
					return "manager";
				}
			}
			naviDao.delete(keyString);
		}
		return "manager";
	}

	public M_Navi getNavi() {
		return navi;
	}

	public void setNavi(M_Navi navi) {
		this.navi = navi;
	}

	public List<M_Navi> getNaviList() {
		naviList = naviDao.getListAll();
		/*for (int i = 0; i < naviList.size(); ++i) {
			if (naviList.get(i).isBoolDisplay())
			System.out.println("true");
		}*/
		return naviList;
	}

	public void setNaviList(List<M_Navi> naviList) {
	}

	public String getKeyString() {
		return keyString;
	}

	public void setKeyString(String keyString) {
		this.keyString = keyString;
	}
}
